package com.zalyyh.advertisement.open

import android.app.Activity
import android.os.Build
import android.util.Log
import android.view.View
import com.bytedance.sdk.openadsdk.*
import com.zalyyh.advertisement.enty.AdaveData
import com.zalyyh.advertisement.interfac.AdverCallback
import com.zalyyh.advertisement.pangolin.Configure
import android.widget.Toast
import kotlin.math.log


class Adavert {
    private var ispreload = false //预加载状态
    private var isShow = false //是否立即显示广告
    private var mTTAdNative: TTAdNative? = null
    private var mttRewardVideoAd: TTRewardVideoAd? = null
    private var mttFullVideoAd : TTFullScreenVideoAd? = null
    private var activity: Activity? = null
    private var callback: AdverCallback? = null
    private var key: Int = 0
    private var a_data: AdaveData? = null
    private var adSlot: AdSlot? = null

    constructor(activity: Activity, callback: AdverCallback) {
        this.activity = activity
        this.callback = callback
        //step2:创建TTAdNative对象，createAdNative(Context context) banner广告context需要传入Activity对象
        mTTAdNative = Configure.get().getManager().createAdNative(activity)
        //step3:(可选，强烈建议在合适的时机调用):申请部分权限，如read_phone_state,防止获取不了imei时候，下载类广告没有填充的问题。
        Configure.get().getManager().requestPermissionIfNecessary(activity)
        a_data = AdaveData()
    }

    /**
     * 0-------->BannerExpress  banner广告
     * 1-------->InteractionExpress
     * 2-------->loadRewardVideoAd  激励视频
     * 3-------->loadNativeExpressAd  信息流
     * 4-------->loadFullScreenVideoAd  全屏视频
     * */
    private fun handlKey(a: AdSlot) {
        if (this.key == 0) {
            loadBannerExpress(a)
        } else if (this.key == 1) {
            loadInteractionExpress(a)
        } else if (this.key == 2) {
            loadRewardVideoAd(a)
        } else if (this.key == 3) {
            loadNativeExpressAd(a)
        }else if (this.key == 4) {
            loadFullScreenVideoAd(a)
        }
    }

    /*预加载广告*/
    fun preload(a: AdSlot?, key: Int) {
        if (ispreload) {
            ispreload = false
        }
        this.key = key
        if (a != null) {
            adSlot = a
        }
        if (adSlot == null) return
        handlKey(adSlot!!)
    }

    /*显示广告*/
    fun show(a: AdSlot, key: Int) {
        if (this.key != 0 && this.key != key) return
        this.adSlot = adSlot
        if (ispreload) {
            ispreload = false
            if (a_data == null) return
            if (callback != null) callback!!.show(a_data!!)
        } else {
            isShow = true
            preload(a, key)
        }
    }

    /*创建广告请求参数AdSlot*/
    fun setAdSlot(id: String, w: Float, h: Float): AdSlot.Builder {
        //step4:创建广告请求参数AdSlot,具体参数含义参考文档
        val adSlot = AdSlot.Builder()
            .setCodeId(id) //广告位id
            .setSupportDeepLink(true)
            .setAdCount(1) //请求广告数量为1到3条
            .setImageAcceptedSize(600, 300)//这个参数设置即可，不影响模板广告的size
        if (w > 0) {
            adSlot.setExpressViewAcceptedSize(w, h) //期望模板广告view的size,单位dp
        }

        return adSlot
    }

    /*加载广告BannerExpress*/
    private fun loadBannerExpress(a: AdSlot) {
        Log.e("zalyyh","loadBannerExpress    开始 ")
        mTTAdNative!!.loadBannerExpressAd(a, getNativeExpressAd(a.codeId))
        Log.e("zalyyh","loadBannerExpress    结束 ")
    }

    /*加载广告InteractionExpress*/
    private fun loadInteractionExpress(a: AdSlot) {
        mTTAdNative!!.loadInteractionExpressAd(a, getNativeExpressAd(a.codeId))
    }

    /*加载激励视频广告*/
    private fun loadRewardVideoAd(a: AdSlot) {
        mTTAdNative!!.loadRewardVideoAd(a, object : TTAdNative.RewardVideoAdListener {
            override fun onError(code: Int, message: String) {
                if (callback != null) callback!!.onError(code, message!!)
            }

            //视频广告加载后的视频文件资源缓存到本地的回调
            override fun onRewardVideoCached() {

            }

            //视频广告素材加载到，如title,视频url等，不包括视频文件
            override fun onRewardVideoAdLoad(ad: TTRewardVideoAd) {
                mttRewardVideoAd = ad
                if (callback != null)
                    mttRewardVideoAd!!.setRewardAdInteractionListener(callback)
                ispreload = true
                if (a_data == null) return
                a_data!!.codeId = a.codeId
                a_data!!.rewardVideoAd = mttRewardVideoAd
                if (isShow) {
                    //需要回调出去 提供显示
                    if (callback != null) callback!!.show(a_data!!)
                }
                isShow = false
            }
        })

    }
/* 加载全屏视频*/
    private fun loadFullScreenVideoAd(a: AdSlot) {
        mTTAdNative!!.loadFullScreenVideoAd(a, object : TTAdNative.FullScreenVideoAdListener {
            override fun onError(code: Int, message: String) {
                if (callback != null) callback!!.onError(code, message!!)
            }
            //视频广告素材加载到，如title,视频url等，不包括视频文件
            override fun onFullScreenVideoAdLoad(ad: TTFullScreenVideoAd?) {
                mttFullVideoAd = ad
                if (callback != null)
                    mttFullVideoAd!!.setFullScreenVideoAdInteractionListener(callback)
                ispreload = true
                if (a_data == null) return
                a_data!!.codeId = a.codeId
                a_data!!.mttFullVideoAd = mttFullVideoAd
                if (isShow) {
                    //需要回调出去 提供显示
                    if (callback != null) callback!!.show(a_data!!)
                }
                isShow = false
            }

            override fun onFullScreenVideoCached() {

            }


        })

    }

    /*加载信息流广告*/
    private fun loadNativeExpressAd(a: AdSlot) {
        mTTAdNative!!.loadNativeExpressAd(a, getNativeExpressAd(a.codeId))
        /*mTTAdNative!!.loadFeedAd(a, object : TTAdNative.FeedAdListener {
            override fun onFeedAdLoad(p0: MutableList<TTFeedAd>?) {
                if (p0 == null || p0.isEmpty()) {
                    return
                }
                for (i in p0) {
                   var  tit =  i.getTitle()
                   var  des =  i.getDescription()
                   var  list =  i.getImageList()
                    for (v in list){
                        var ic = v.imageUrl
                    }
                }

            }

            override fun onError(p0: Int, p1: String?) {
                Log.e("zalyyh","code:   $p0    message: $p1")

            }

        })*/
    }

    private fun getNativeExpressAd(id: String): TTAdNative.NativeExpressAdListener {
        return object : TTAdNative.NativeExpressAdListener {
            override fun onNativeExpressAdLoad(p0: MutableList<TTNativeExpressAd>?) {
                if (p0 == null || p0.size == 0) {
                    return
                }
                var a = p0[0]
                a.setExpressInteractionListener(object :
                    TTNativeExpressAd.ExpressAdInteractionListener {
                    override fun onAdClicked(p0: View?, p1: Int) {

                    }

                    override fun onAdShow(p0: View?, p1: Int) {

                    }

                    /*渲染成功 可以显示广告 返回view的宽高 单位 dp*/
                    override fun onRenderSuccess(p0: View?, p1: Float, p2: Float) {
                        ispreload = true
                        if (a_data == null) return
                        a_data!!.view = p0
                        a_data!!.wide = p1
                        a_data!!.high = p2
                        a_data!!.codeId = id
                        if (isShow) {
                            //需要回调出去 提供显示
                            if (callback != null) callback!!.show(a_data!!)
                        }
                        isShow = false
                    }

                    override fun onRenderFail(p0: View?, p1: String?, p2: Int) {

                    }

                })
                a.render()
                if (callback != null) callback!!.onNativeExpressAdLoad(p0!!)
            }

            override fun onError(p0: Int, p1: String?) {
                if (callback != null) callback!!.onError(p0, p1!!)
            }

        }
    }

    /*回收资源*/
    fun clear() {
        ispreload = false
        isShow = false
        mttRewardVideoAd = null
        mTTAdNative = null
        activity = null
    }


}